﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class PointGift
    {
        public int Add(Chain.Model.PointGift model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into PointGift(");
            builder.Append("GiftName,GiftCode,GiftClassID,GiftPhoto,GiftExchangePoint,GiftStockNumber,GiftExchangeNumber,GiftShopID,GiftRemark)");
            builder.Append(" values (");
            builder.Append("@GiftName,@GiftCode,@GiftClassID,@GiftPhoto,@GiftExchangePoint,@GiftStockNumber,@GiftExchangeNumber,@GiftShopID,@GiftRemark)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GiftName", SqlDbType.NVarChar, 50), new SqlParameter("@GiftCode", SqlDbType.VarChar, 50), new SqlParameter("@GiftClassID", SqlDbType.Int, 4), new SqlParameter("@GiftPhoto", SqlDbType.NVarChar, 500), new SqlParameter("@GiftExchangePoint", SqlDbType.Int, 4), new SqlParameter("@GiftStockNumber", SqlDbType.Int, 4), new SqlParameter("@GiftExchangeNumber", SqlDbType.Int, 4), new SqlParameter("@GiftShopID", SqlDbType.Int, 4), new SqlParameter("@GiftRemark", SqlDbType.NVarChar, 500) };
            cmdParms[0].Value = model.GiftName;
            cmdParms[1].Value = model.GiftCode;
            cmdParms[2].Value = model.GiftClassID;
            cmdParms[3].Value = model.GiftPhoto;
            cmdParms[4].Value = model.GiftExchangePoint;
            cmdParms[5].Value = model.GiftStockNumber;
            cmdParms[6].Value = model.GiftExchangeNumber;
            cmdParms[7].Value = model.GiftShopID;
            cmdParms[8].Value = model.GiftRemark;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int GiftID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from PointGift ");
            builder.Append(" where GiftID=@GiftID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GiftID", SqlDbType.Int, 4) };
            cmdParms[0].Value = GiftID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string GiftIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from PointGift ");
            builder.Append(" where GiftID in (" + GiftIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(string GiftName, int GiftID, int shopID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from PointGift");
            builder.Append(" where GiftName=@GiftName and GiftShopID=@GiftShopID and GiftID<>@GiftID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GiftName", SqlDbType.NVarChar, 50), new SqlParameter("@GiftShopID", SqlDbType.Int), new SqlParameter("@GiftID", SqlDbType.Int) };
            cmdParms[0].Value = GiftName;
            cmdParms[1].Value = shopID;
            cmdParms[2].Value = GiftID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool ExistsAdd(string GiftName, int ShopID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from PointGift");
            builder.Append(" where GiftName=@GiftName and GiftShopID=@GiftShopID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GiftName", SqlDbType.NVarChar, 50), new SqlParameter("@GiftShopID", SqlDbType.Int) };
            cmdParms[0].Value = GiftName;
            cmdParms[1].Value = ShopID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetItemModel(int GiftID)
        {
            return DbHelperSQL.Query("select * from PointGift where GiftID=" + GiftID);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select GiftID,GiftName,GiftCode,GiftClassID,GiftPhoto,GiftExchangePoint,GiftStockNumber,GiftExchangeNumber,GiftShopID,GiftRemark ");
            builder.Append(" FROM PointGift ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" GiftID,GiftName,GiftCode,GiftClassID,GiftPhoto,GiftExchangePoint,GiftStockNumber,GiftExchangeNumber,GiftShopID,GiftRemark ");
            builder.Append(" FROM PointGift ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.GiftID desc");
            }
            builder.Append(")AS Row, T.*  from PointGift T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListS(int PageSize, int PageIndex, string[] strWhere, out int resCount)
        {
            string tableName = "PointGift";
            string[] column = new string[] { "*" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "GiftID", true, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public DataSet GetListSP(int PageSize, int PageIndex, bool IsAsc, string[] strWhere, out int resCount)
        {
            string tableName = "PointGift,SysShop,GiftClass";
            string[] column = new string[] { "PointGift.*,SysShop.ShopName,GiftClassName" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "GiftExchangePoint", "GiftID", IsAsc, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("GiftID", "PointGift");
        }

        public Chain.Model.PointGift GetModel(int GiftID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 GiftID,GiftName,GiftCode,GiftClassID,GiftPhoto,GiftExchangePoint,GiftStockNumber,GiftExchangeNumber,GiftShopID,GiftRemark from PointGift ");
            builder.Append(" where GiftID=@GiftID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GiftID", SqlDbType.Int, 4) };
            cmdParms[0].Value = GiftID;
            Chain.Model.PointGift gift = new Chain.Model.PointGift();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["GiftID"] != null) && (set.Tables[0].Rows[0]["GiftID"].ToString() != ""))
            {
                gift.GiftID = int.Parse(set.Tables[0].Rows[0]["GiftID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["GiftName"] != null) && (set.Tables[0].Rows[0]["GiftName"].ToString() != ""))
            {
                gift.GiftName = set.Tables[0].Rows[0]["GiftName"].ToString();
            }
            if ((set.Tables[0].Rows[0]["GiftCode"] != null) && (set.Tables[0].Rows[0]["GiftCode"].ToString() != ""))
            {
                gift.GiftCode = set.Tables[0].Rows[0]["GiftCode"].ToString();
            }
            if ((set.Tables[0].Rows[0]["GiftClassID"] != null) && (set.Tables[0].Rows[0]["GiftClassID"].ToString() != ""))
            {
                gift.GiftClassID = int.Parse(set.Tables[0].Rows[0]["GiftClassID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["GiftPhoto"] != null) && (set.Tables[0].Rows[0]["GiftPhoto"].ToString() != ""))
            {
                gift.GiftPhoto = set.Tables[0].Rows[0]["GiftPhoto"].ToString();
            }
            if ((set.Tables[0].Rows[0]["GiftExchangePoint"] != null) && (set.Tables[0].Rows[0]["GiftExchangePoint"].ToString() != ""))
            {
                gift.GiftExchangePoint = int.Parse(set.Tables[0].Rows[0]["GiftExchangePoint"].ToString());
            }
            if ((set.Tables[0].Rows[0]["GiftStockNumber"] != null) && (set.Tables[0].Rows[0]["GiftStockNumber"].ToString() != ""))
            {
                gift.GiftStockNumber = int.Parse(set.Tables[0].Rows[0]["GiftStockNumber"].ToString());
            }
            if ((set.Tables[0].Rows[0]["GiftExchangeNumber"] != null) && (set.Tables[0].Rows[0]["GiftExchangeNumber"].ToString() != ""))
            {
                gift.GiftExchangeNumber = int.Parse(set.Tables[0].Rows[0]["GiftExchangeNumber"].ToString());
            }
            if ((set.Tables[0].Rows[0]["GiftShopID"] != null) && (set.Tables[0].Rows[0]["GiftShopID"].ToString() != ""))
            {
                gift.GiftShopID = int.Parse(set.Tables[0].Rows[0]["GiftShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["GiftRemark"] != null) && (set.Tables[0].Rows[0]["GiftRemark"].ToString() != ""))
            {
                gift.GiftRemark = set.Tables[0].Rows[0]["GiftRemark"].ToString();
            }
            return gift;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM PointGift ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public int Update(Chain.Model.PointGift model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update PointGift set ");
            builder.Append("GiftName=@GiftName,");
            builder.Append("GiftCode=@GiftCode,");
            builder.Append("GiftClassID=@GiftClassID,");
            builder.Append("GiftPhoto=@GiftPhoto,");
            builder.Append("GiftExchangePoint=@GiftExchangePoint,");
            builder.Append("GiftStockNumber=@GiftStockNumber,");
            builder.Append("GiftExchangeNumber=@GiftExchangeNumber,");
            builder.Append("GiftShopID=@GiftShopID,");
            builder.Append("GiftRemark=@GiftRemark");
            builder.Append(" where GiftID=@GiftID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GiftName", SqlDbType.NVarChar, 50), new SqlParameter("@GiftCode", SqlDbType.VarChar, 50), new SqlParameter("@GiftClassID", SqlDbType.Int, 4), new SqlParameter("@GiftPhoto", SqlDbType.NVarChar, 500), new SqlParameter("@GiftExchangePoint", SqlDbType.Int, 4), new SqlParameter("@GiftStockNumber", SqlDbType.Int, 4), new SqlParameter("@GiftExchangeNumber", SqlDbType.Int, 4), new SqlParameter("@GiftShopID", SqlDbType.Int, 4), new SqlParameter("@GiftID", SqlDbType.Int, 4), new SqlParameter("@GiftRemark", SqlDbType.NVarChar, 500) };
            cmdParms[0].Value = model.GiftName;
            cmdParms[1].Value = model.GiftCode;
            cmdParms[2].Value = model.GiftClassID;
            cmdParms[3].Value = model.GiftPhoto;
            cmdParms[4].Value = model.GiftExchangePoint;
            cmdParms[5].Value = model.GiftStockNumber;
            cmdParms[6].Value = model.GiftExchangeNumber;
            cmdParms[7].Value = model.GiftShopID;
            cmdParms[8].Value = model.GiftID;
            cmdParms[9].Value = model.GiftRemark;
            if (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0)
            {
                return 1;
            }
            return -3;
        }

        public int UpdateGiftNumber(int giftID, int number)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update PointGift set GiftStockNumber=GiftStockNumber-@GiftExchangeNumber,");
            builder.Append("GiftExchangeNumber=GiftExchangeNumber+@GiftExchangeNumber ");
            builder.Append(" where GiftID=@GiftID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GiftExchangeNumber", SqlDbType.Int, 4), new SqlParameter("@GiftID", SqlDbType.Int, 4) };
            cmdParms[0].Value = number;
            cmdParms[1].Value = giftID;
            return DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
        }
    }
}

